<?php
class ContentType extends CI_Model {
	
	public $table;
	public $name;
	public $plural;
	public $data;

	public function init( $type )
	{
		
		$this->load->helper('inflector');
		
		$this->plural = $type;
		$this->name = $type;
		
		$this->load->model($this->name);
		$this->table = $this->$type->ctable;
		
	}
	
	public function getAll()
	{
		$type = $this->name;
		$this->data = $this->$type->getAll();
	}
	
	public function getWithID($id)
	{
		$type = $this->name;
		$this->data = $this->$type->getWithID($id);
	}
	
	public function show()
	{
		
		$this->load->View('admin/addlink',array('name'=>$this->name));
		
		//Get all of the data for the current content type.
		$this->getAll();
		$this->load->Model('admin/contentList');
		
		foreach ( $this->data as $k=>$v ) {
			$this->data[$k]->content = mb_substr(strip_tags(html_entity_decode($v->content,ENT_QUOTES,'UTF-8')),0,150);
		}
		
		$listFields = array('title','content','name','link', 'sticky');
		
		if ( count ($this->data) > 0 ) { 
			$this->contentList->setFields($listFields); 
			$this->contentList->init( $this->name, array_keys((Array)$this->data[0]), $this->data );
			$this->contentList->draw();
		} else {
			//TODO::display not content available error message.
		}
		//draw a table  
	}
	
	public function add()
	{
		//Draw a blank form
		
		$this->load->Model('admin/ContentForm');
		
		$type = $this->name;
		$this->ContentForm->init( $this->$type->parseStructureForForm(), $type );
		$this->ContentForm->process();
		
		if ( $this->ContentForm->isValid() === true ) {
			$this->save($_POST, $this->$type->getParsedFields() );
			
		}
		
		$this->load->View('admin/pageView',array('content'=>$this->ContentForm->draw(array())));
		
	}

	public function edit ( $id )
	{
		$this->load->Model('admin/ContentForm');
		
		$type = $this->name;
		
		$data = $this->$type->getForForm((int)$id);
		
		if ( count($data) > 0 ) {
			$this->ContentForm->init( $this->$type->parseStructureForForm(), $type );
			
			$this->ContentForm->process(true);
			
			if ( $this->ContentForm->isValid() === true ) {
					$this->save($_POST, $this->$type->getParsedFields(), $id );
			}
			
			$data = $this->$type->getForForm((int)$id);
			$data = (Array)$data[0];
			$this->load->View('admin/pageView',array('content'=>$this->ContentForm->draw($data)));
		} else {
			//TODO::Echo content not found error.
		}
	}
	
	
	public function delete($id) {
		$sql = 'DELETE FROM ' . $this->table . ' WHERE uid=?';
		$this->db->query($sql,array($id));
		$this->load->helper('url');
		header('Location:' . admin::$adminURL . 'show/' . $this->name . '/');
	}
	
	
	public function save ( $content, $fields,  $id = false ) 
	{
		
		//If we have been given an id number then update the row with the current content.
		$type = $this->name;
		$this->load->Model($this->name);
		$this->load->helper('url');
		
		foreach ( $content as $key=>$val ) {
			if ( !in_array($key,$fields) ) {
				unset($content[$key]);
			} else {
				$markers[] = '?';
				if ( stripos($key,'timestamp') !== false ) {
					
					if(!isset($val[0])) { $val[0] = 'Jan';}
					if(!isset($val[1])) { $val[1] = 2; }
					if(!isset($val[2])) { $val[2] = 1970; }
					
					$timestamp = strtotime(implode('-',$val));
					if ($timestamp === false) { $timestamp = 1000000000;}
					
					$content[$key] = $timestamp;
					
				}
 			}
			//TODO:: Current logged in user.
		}
		ini_set('display_errors',1);
		error_reporting(E_ALL|E_STRICT);
		//If no specific id is set then we are adding, otherwise we are updating.
		if ( $id === false ) {
			
			if ( isset($content['uid']) ) { unset($content['uid']); }
			
			$sql = 'INSERT INTO ' . $this->table . ' (' . implode(',',array_keys($content)) . ') VALUES (' . implode(',',$markers) . ');';
			$this->db->query($sql,$content);
			$id = $this->db->insert_id();
			$obj = $this->$type->makeFromID($id);
			$obj->saveCallback();
			
			//Redirect to the edit view for this item.
			header('Location:' . admin::$adminURL . 'edit/' . $this->name . '/' . $id . '/');
			
		} else {
			
			$keys = array_keys($content);
			foreach ($markers as $key=>$value) {
				$markers[$key] = $keys[$key] . '= ?'; 
			}
			
			$sql = 'UPDATE ' . $this->table . ' SET ' . implode(',', $markers) . ' WHERE uid=' . $id;
			
			$this->db->query($sql,$content);
			$obj = $this->$type->makeFromID($id);
			$obj->saveCallback();
		}
		
	}
	
	
}